package com.example.q_kang.hhh.setting;


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.util.SparseArrayCompat;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

import com.example.q_kang.hhh.R;
import com.hjq.toast.ToastUtils;
import com.jakewharton.rxbinding2.widget.RxAdapterView;
import com.sskj.base.MVPActivity;
import com.sskj.base.adapter.ArrowAdapter;
import com.sskj.base.api.SpConfig;
import com.sskj.base.base.App;
import com.sskj.base.bean.BankTypes;
import com.sskj.base.bean.MyBanksBean;
import com.sskj.base.utils.CheckUtil;
import com.sskj.base.utils.ClickUtil;
import com.sskj.base.utils.SPUtils;
import com.sskj.base.view.ToolBarLayout;

import java.nio.channels.InterruptedByTimeoutException;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * @author Hey
 * Create at  2019/04/10
 */
public class SetBankActivity extends MVPActivity<SetBankPresenter> {

    @BindView(R.id.toolbar)
    ToolBarLayout toolbar;
    @BindView(R.id.et_real_name)
    EditText etRealName;
    @BindView(R.id.et_bank_card)
    EditText etBankCard;
    @BindView(R.id.spinner)
    Spinner spinner;
    @BindView(R.id.et_beanch_bank)
    EditText etBeanchBank;
    @BindView(R.id.et_code)
    EditText etCode;
    @BindView(R.id.tv_get_code)
    TextView tvGetCode;
    @BindView(R.id.btn_submit)
    Button btnSubmit;

    private List<BankTypes> banks = new ArrayList<>();
    private String bankId;
    private ArrowAdapter spinnerTypeAdapter;
    private MyBanksBean.BankBean bankBean;

    @Override
    public int getLayoutId() {
        return R.layout.activity_set_bank;
    }

    @Override
    public SetBankPresenter getPresenter() {
        return new SetBankPresenter();
    }

    @Override
    public void initView() {
        toolbar.setLeftButtonOnClickListener(view -> finish());
        BankTypes bankTypes = new BankTypes();
        bankTypes.setName_cn(App.INSTANCE.getString(R.string.appJavaSetBankActivity1));
        banks.add(bankTypes);
        spinnerTypeAdapter = new ArrowAdapter(this,
                R.layout.spinner_item_up, banks);
        spinnerTypeAdapter.setDropDownViewResource(R.layout.spinner_item_down);
        spinner.setAdapter(spinnerTypeAdapter);
        spinner.setSelection(0);
        spinner.setDropDownVerticalOffset(10);
        RxAdapterView.itemSelections(spinner)
                .subscribe(pos -> {
                    if (pos == 0) {
                        bankId = null;
                    } else {
                        bankId = banks.get(pos).getId();
                    }
                });
        ClickUtil.click(300, tvGetCode, view -> mPresenter.sendCode((String) SPUtils.get(App.INSTANCE, SpConfig.MOBILE, "")));
        ClickUtil.click(300, btnSubmit, view -> {
            if (TextUtils.isEmpty(etRealName.getText().toString())) {
                ToastUtils.show(App.INSTANCE.getString(R.string.appJavaSetBankActivity3));
                return;
            }
            if (TextUtils.isEmpty(etBankCard.getText().toString())) {
                ToastUtils.show(App.INSTANCE.getString(R.string.appJavaSetBankActivity4));
                return;
            }
            if (TextUtils.isEmpty(etBeanchBank.getText().toString())) {
                ToastUtils.show(App.INSTANCE.getString(R.string.appJavaSetBankActivity5));
                return;
            }
            if (bankId == null) {
                ToastUtils.show(App.INSTANCE.getString(R.string.appJavaSetBankActivity1));
                return;
            }
            if (TextUtils.isEmpty(etCode.getText().toString())) {
                ToastUtils.show(App.INSTANCE.getString(R.string.appJavaSetBankActivity7));
                return;
            }
            showLoading();
            if (bankBean == null) {
                mPresenter.bindBank(etRealName.getText().toString(),
                        etBankCard.getText().toString(),
                        bankId,
                        etBeanchBank.getText().toString(),
                        etCode.getText().toString());
            } else {
                mPresenter.editBank(etRealName.getText().toString(),
                        etBankCard.getText().toString(),
                        bankId,
                        etBeanchBank.getText().toString(),
                        etCode.getText().toString(),
                        (String) SPUtils.get(App.INSTANCE, SpConfig.MOBILE, ""));
            }

        });

    }

    @Override
    public void initData() {
        bankBean = getIntent().getExtras().getParcelable("bank");
        if (bankBean != null) {
            etRealName.setText(bankBean.getBank_name());
            etBankCard.setText(bankBean.getBank_account());
            etBeanchBank.setText(bankBean.getBank_address());
            BankTypes bankTypes = new BankTypes();
            bankTypes.setName_cn(bankBean.getName_cn());
            bankTypes.setId(bankBean.getOpening_id());
            bankId = bankBean.getId();
            banks.clear();
            banks.add(bankTypes);
            RxAdapterView.itemSelections(spinner)
                    .subscribe(pos -> {
                    });
        }
        if (bankBean == null) {
            mPresenter.getAddBankList();
        }
    }

    public static void start(Context context, MyBanksBean.BankBean bankBean) {
        Intent intent = new Intent(context, SetBankActivity.class);
        Bundle bundle = new Bundle();
        bundle.putParcelable("bank", bankBean);
        intent.putExtras(bundle);
        context.startActivity(intent);
    }

    public void sendCodeSuccess() {
        startTimeDown(tvGetCode);
    }

    public void showBnaks(List<BankTypes> data) {
        banks.addAll(data);
        spinnerTypeAdapter.notifyDataSetChanged();
        if (bankBean != null) {
            for (int i = 1; i < banks.size(); i++) {
                if (banks.get(i).getId().equals(bankBean.getOpening_id())) {
                    spinner.setSelection(i);
                    return;
                }
            }
        }
    }
}
